<template>
	<view class="product-settle-address-container">
		<view class="product-settle-address-upper">
			外卖配送
		</view>
		<view class="choose" @tap="showAddress(0)" v-if="data.address.addressIndex == -1">
			<view class="choose-text">点击选择地址</view>
			<image src="../../static/go.png" mode="widthFix"></image>
		</view>
		<view class="product-settle-address-show" v-if="data.address.addressIndex!=-1">
			<view class="product-settle-address-show-item" @tap="showAddress(0)">
				<view class="product-settle-address-show-item-address">
					{{data.address.addressList[data.address.addressIndex].address}}
				</view>
				<view class="product-settle-address-show-item-region">
					{{data.address.addressList[data.address.addressIndex].area}}
				</view>
				<view class="product-settle-address-show-item-other">
					<view class="product-settle-address-show-item-name">
						{{data.address.addressList[data.address.addressIndex].name}}
					</view>
					<view class="product-settle-address-show-item-phone">
						{{data.address.addressList[data.address.addressIndex].phone}}
					</view>
				</view>
				<image src="../../static/go.png" mode="widthFix"></image>
			</view>
		</view>
		<u-popup :show="data.address.addressShow" @close="showAddress(1) ">
			<view class="product-settle-address-selector" :style="{'height':windowHeight*2/3+'px'}">
				<scroll-view scroll-y="true" :style="{'height':windowHeight*2/3-btnHeight +'px'}">
					<view class="product-settle-address-select-item" v-for="(item,index) in data.address.addressList" :key="index" @tap="setAddressIndex(index)">
						<view style="width: 600rpx;">
							<view class="product-settle-address-select-item-address">
								{{item.address}}
							</view>
							<view class="product-settle-address-select-item-region">
								{{item.area}}
							</view>
							<view class="product-settle-address-select-item-other">
								<view class="product-settle-address-select-item-name">
									{{item.name}}
								</view>
								<view class="product-settle-address-select-item-phone">
									{{item.phone}}
								</view>
							</view>
						</view>
						<image src="../../static/go.png" mode="widthFix"></image>
					</view>
				</scroll-view>
				<button class="product-settle-address-selector-btn" @tap="goAddress">新增/修改地址</button>
			</view>
		</u-popup>
	</view>
	<view class="product-settle-product-container">
		<view class="product-settle-product-item" v-for="(item,index) in data.settleInfo" :key="index">
			<view v-if="item.count!=0" style="display: flex;">
				<image :src="item.productInfo.pic_url" mode="aspectFit"></image>
				<view class="product-settle-product-item-text">
					<view class="product-settle-product-item-name">
						{{item.productInfo.product_name}}
					</view>
					<view class="product-settle-product-item-count">
						<text style="font-size: 25rpx;">x</text>{{item.count}}
					</view>
				</view>
				<view class="product-settle-product-item-price">
					<text style="font-weight: normal; font-size: 25rpx;">￥</text>{{item.sumPrice}}
				</view>
			</view>
		</view>
		<view class="product-settle-product-whole">
			<text style="font-size: 30rpx;">小计 ￥</text><text style="font-size: 45rpx; font-weight: bold;">{{data.price}}</text>
		</view>
	</view>
	<view class="product-settle-note-container">
		<view class="product-settle-note-item" @tap="showInput(0)">
			<view class="product-settle-note-label">备注</view>
			<view class="product-settle-note-value">{{data.note.outShow}}</view>
		</view>
		<u-popup :show="data.note.noteShow" mode="bottom" @close="closeInput" closeOnClickOverlay="true">
			<view class="product-settle-note-input-container">
				<u--textarea border="surround" height="200rpx" v-model="data.note.noteContent" placeholder="请输入备注" count></u--textarea>
				<button class="product-settle-note-btn" @tap="showInput(1)">确认</button>
			</view>
		</u-popup>
	</view>
	<view class="product-settle-pay-container">
		<radio-group name="pay-group" @change="changePayMethod" class="product-settle-pay-group">
			<label class="product-settle-pay-item">
				<view class="product-settle-pay-item-label">
					<image src="/page_product/static/wx-pay.png" mode="aspectFit"></image>
					<view class="product-settle-pay-item-text">微信支付</view>
				</view>
				<radio :value="0" :checked="data.payIndex == 0"/>
			</label>
			<label class="product-settle-pay-item">
				<view class="product-settle-pay-item-label">
					<image src="/page_product/static/zfb-pay.png" mode="aspectFit"></image>
					<view class="product-settle-pay-item-text">支付宝支付</view>
				</view>
				<radio :value="1" :checked="data.payIndex == 1" />
			</label>
		</radio-group>
	</view>
	<view class="product-foot-container">
		<view class="product-foot-price">
			<text>合计￥</text>{{data.price}}
		</view>
		<button class="product-foot-btn" @tap="goSettle">提交订单</button>
	</view>
</template>

<script>
	import {onMounted,reactive,ref} from "vue"
	import {onLoad, onShow} from '@dcloudio/uni-app'
	import global from "../../config/global.js"
	import {Decimal} from 'decimal.js'
	import {cal} from '../../utils/cal.js'
	export default {
		name:"product-settle",
		setup(){
			
			let data = reactive({
				address:{
					addressList:[],
					addressShow:false,
					addressIndex:-1
				},
				note:{
					noteContent:"",
					outShow:"请填写备注",
					noteShow:false 
				},
				payIndex:0,
				settleInfo:[],
				price:0
			})
			
			let windowHeight = uni.getSystemInfoSync()['windowHeight'];
			let btnHeight = uni.upx2px(120);
			
			onLoad((e) =>{
				data.settleInfo = JSON.parse(decodeURIComponent(e.settleInfo))
				data.price = new Decimal(0)
				for(let item of data.settleInfo){
					console.log(item)
					item.sumPrice = cal.cheng(item.count,item.productInfo.discount_price)
					data.price = cal.jia(data.price,item.sumPrice)
				}
				console.log(data.settleInfo,data.price,typeof(data.price))
			})
			
			onMounted(()=>{
				getAddress()
			})
			
			onShow(()=>{
				getAddress()
			})
			
			function getAddress(){
				uni.request({
					url:global.BASE_URL+'/user/myaddress',
					method:"GET",
					header:{
						"mytoken":uni.getStorageSync('localtoken')
					},
					success:(res)=>{		
						if(res.data.code){
							data.address.addressList = res.data.data
						}
					},
					fail:(err)=>{
						console.log(err)
					}
				})
			}
			
			function showAddress(v){
				if(v==0||v==1){
					data.address.addressShow = !data.address.addressShow
				}
			}
			
			function goAddress(){
				uni.navigateTo({
					url:'/pages/me/my-address'
				})
			}
			
			function setAddressIndex(v){
				data.address.addressIndex = v;
				data.address.addressShow = false
			}
			
			function showInput(v){
				if(v==0){
					data.note.noteContent = (data.note.outShow == '请填写备注'?"":data.note.outShow)
					data.note.noteShow = true
				}else{
					data.note.outShow = (data.note.noteContent == ''?'请填写备注':data.note.noteContent)
					data.note.noteShow = false
				}
			}
			
			function closeInput(){
				data.note.noteShow = false
			}
			
			function changePayMethod(v){
				data.payIndex = v
			}
			
			return {
				data,
				windowHeight,
				btnHeight,
				showAddress,
				goAddress,
				setAddressIndex,
				showInput,
				closeInput,
				changePayMethod
			}
			
		}
	}
</script>

<style lang="scss" scoped>
	
	.product-settle-address-container{
		width: 680rpx;
		margin: 10rpx 35rpx;
		border-radius: 20rpx;
		border: 2rpx solid #515151;
		
		& .product-settle-address-upper{
			width: 640rpx;
			padding: 0 20rpx;
			height: 60rpx;
			line-height: 60rpx;
			border-bottom: 1rpx solid #888888;
			text-align: center;
		}
		
		& .product-settle-address-show{
			margin: 10rpx 0rpx;
			position: relative;
			
			& .product-settle-address-show-item{
				position: relative;
				margin: 10rpx 5rpx;
				// align-items: center;
				
				& .product-settle-address-show-item-address{
					width: 540rpx;
					font-size: 35rpx;
					font-weight: bold;
				}
				
				& .product-settle-address-show-item-region{
					margin-top: 5rpx;
					font-size: 28rpx;
					color:#888888;
				}
				
				& image{
					width: 40rpx;
					position: absolute;
					right: 20rpx;
					top: 10rpx;
				}
				
				& .product-settle-address-show-item-other{
					margin-top: 5rpx;
					display: flex;
					
					& .product-settle-address-show-item-name{
						font-size: 28rpx;
						color: #888888;
					}
					
					& .product-settle-address-show-item-phone{
						font-size: 28rpx;
						color: #888888;
						margin-left: 20rpx;
					}
				}
			}
		}
		
		& .product-settle-address-selector{
			width: 660rpx;
			margin: 15rpx 45rpx;
			
			& .product-settle-address-selector-btn{
				width: 450rpx;
				height: 70rpx;
				line-height: 70rpx;
				margin-top: 15rpx;
				color: white;
				background-color: #9BCDD2;
			}
			
			& .product-settle-address-select-item{
				position: relative;
				width: 640rpx;
				padding: 7rpx 10rpx;
				margin: 10rpx 0;
				border-bottom: 2rpx solid #aaaaaa;
				display: flex;
				align-items: center;
				
				& image{
					width: 40rpx;
					position: absolute;
					right: 20rpx;
				}
				
				& .product-settle-address-select-item-address{
					font-size: 35rpx;
					font-weight: bold;
				}
				
				& .product-settle-address-select-item-region{
					margin-top: 5rpx;
					font-size: 28rpx;
					color:#888888;
				}
				
				& .product-settle-address-select-item-other{
					margin-top: 5rpx;
					display: flex;
					
					& .product-settle-address-select-item-name{
						font-size: 28rpx;
						color: #888888;
					}
					
					& .product-settle-address-select-item-phone{
						font-size: 28rpx;
						color: #888888;
						margin-left: 20rpx;
					}
				}
			}
			
		}
		
		& .choose{
			margin: 10rpx 0rpx;
			display: flex;
			height: 70rpx;
			padding: 10rpx 0;
			position: relative;
			align-items: center;
			
			& .choose-text{
				font-size: 32rpx;
				height: 70rpx;
				line-height: 70rpx;
				margin-left: 10rpx;
			}
			
			& image{
				position: absolute;
				width: 30rpx;
				right: 20rpx;
			}
			
			& input{
				width: 450rpx;
				padding: 5rpx 10rpx;
				border-bottom: 2rpx solid #888888;
				// background-color: aqua;
			}
			
			& .choose-label{
				width:150rpx;
				text-align: center;
				font-weight: bold;
			}
		}
	}
	
	.product-settle-product-container{
		width: 680rpx;
		margin: 10rpx 35rpx;
		border-radius: 20rpx;
		border: 2rpx solid #515151;
		
		& .product-settle-product-item{
			position: relative;
			display: flex;
			margin: 5rpx 0 5rpx 10rpx;
			
			& image{
				width: 190rpx;
				height: 150rpx;
			}
			
			& .product-settle-product-item-text{
				margin-left: 10rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-around;
				
				
				& .product-settle-product-item-name{
					width: 380rpx;
					overflow: hidden;
					white-space: nowrap; 
					text-overflow: ellipsis;
					font-size: 32rpx;
					font-weight: bold;
				}
				
				& .product-settle-product-item-count{
					color: #ababab;
					padding: 4rpx 8rpx;
					border: 2rpx solid #ababab;
					height: 30rpx;
					line-height: 30rpx;
					width: fit-content;
					font-size: 28rpx;
					border-radius: 10rpx;
				}
			}
			
			& .product-settle-product-item-price{
				position: absolute;
				bottom: 15rpx;
				right: 20rpx;
				font-weight: bold;
				font-size: 32rpx;
			}
			
		}
		
		& .product-settle-product-whole{
			width: 630rpx;
			margin: 15rpx 25rpx 17rpx 25rpx;
			padding-top: 15rpx;
			border-top: 2rpx solid #cdcdcd;
			text-align: right;
		}
		
	}
	
	.product-settle-note-container{
		width: 680rpx;
		margin: 10rpx 35rpx;
		border-radius: 20rpx;
		border: 2rpx solid #515151;
		
		& .product-settle-note-input-container{
			width: 660rpx;
			margin: 15rpx 45rpx;
			height: 400rpx;
			
			& .product-settle-note-btn{
				height: 70rpx;
				line-height: 70rpx;
				width: 400rpx;
				margin: 55rpx 130rpx 10rpx 130rpx;
				color: white;
				background-color: #9BCDD2;
			}
		}
		
		& .product-settle-note-item{
			display: flex;
			height: 70rpx;
			padding: 10rpx 25rpx;
			align-items: center;
			
			& .product-settle-note-label{
				width: 150rpx;
				font-weight: bold;
				font-size: 32rpx;
			}
			
			& .product-settle-note-value{
				width: 470rpx;
				overflow: scroll;
				text-align: right;
				color: #515151;
			}
		}
	}
	
	.product-settle-pay-container{
		width: 680rpx;
		margin: 10rpx 35rpx;
		border-radius: 20rpx;
		border: 2rpx solid #515151;
		margin-bottom: 130rpx;
		
		& .product-settle-pay-group{
			padding: 15rpx 25rpx;
			
			& .product-settle-pay-item{
				display: flex;
				height: 70rpx;
				align-items: center;
				position: relative;
				
				& .product-settle-pay-item-label{
					display: flex;
					
					& image{
						width: 40rpx;
						height: 40rpx;
					}
					
					& .product-settle-pay-item-text{
						font-size: 26rpx;
						width: 150rpx;
						margin-left: 15rpx;
						font-weight: bold;
					}
				}
				
				& radio{
					position: absolute;
					right: 10rpx;
				}
			}
		}
	}

	.product-foot-container{
		height: 115rpx;
		width: 750rpx;
		background-color: #fff;
		position: fixed;
		bottom: 0;
		display: flex;
		align-items: center;
		
		
		& .product-foot-price{
			text-align: right;
			width: 510rpx;
			font-size: 45rpx;
			font-weight: bold;
			
			& text{
				font-size: 25rpx;
				font-weight: normal;
			}
		}
		
		& .product-foot-btn{
			float: right;
			width: 200rpx;
			color: white;
			height: 75rpx;
			line-height: 75rpx;
			border-radius: 200rpx;
			background-color: #9BCDD2;
		}
	}

</style>
